﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace CreditCard
{
    public partial class AdminHome : Form
    {
        public AdminHome()
        {
            InitializeComponent();
        }
        public AdminHome(AppHome app,Admin admin)
        {
            InitializeComponent();
            this.app = app;
            this.admin = admin;
            get();
        }

        Admin admin;

        AppHome app;

        MySqlDao sqlDao = new MySqlDao();


        List<BaseCard> list;

        bool e1 = true, e2 = true;
        
        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if(e.TabPageIndex == 0)
            {
                get();
            }
            else if(e.TabPageIndex == 1)
            {
                get2();
            }
            else
            {
                if (MessageBox.Show("确定要退出？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    app.openForm(new Home(app));
            }
        }

        private void get()
        {
            DataSet ds = sqlDao.selectCard(0);
            if (ds.Tables[0].Rows.Count > 0)
            {
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
                if (e1)
                {
                    DataGridViewButtonColumn column = new DataGridViewButtonColumn();
                    column.Text = "取 消";
                    column.Name = "编 辑";
                    column.UseColumnTextForButtonValue = true;
                    dataGridView1.Columns.Add(column);

                    column = new DataGridViewButtonColumn();
                    column.Text = "同 意";
                    column.Name = "编 辑";
                    column.UseColumnTextForButtonValue = true;
                    dataGridView1.Columns.Add(column);
                    e1 = false;
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 0 && e.ColumnIndex != 1)
                return;
            string c_id = dataGridView1.Rows[e.RowIndex].Cells["卡号"].Value.ToString();
            string u_id = dataGridView1.Rows[e.RowIndex].Cells["申请人"].Value.ToString();

            Msg msg = new Msg();
            msg.M_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            msg.M_id = Utils.getTimeTicks();
            msg.U_id = u_id;
            //信用卡申请
            if (e.ColumnIndex == 0)
            {
                //取消
                if (MessageBox.Show("确定要驳回该请求？","提示",MessageBoxButtons.YesNo)==DialogResult.Yes 
                    && sqlDao.updateCardState(c_id, -1))
                {
                    msg.M_msg = "尊敬的用户您好！您申请的信用卡（卡号：" + c_id + "）未通过审核。";
                    if (sqlDao.insertMsg(msg))
                    {
                        MessageBox.Show("已驳回...");
                        dataGridView1.Rows.RemoveAt(e.RowIndex);
                    }
                    else
                    {
                        MessageBox.Show("驳回失败...");
                    }
                }
            }
            else if (e.ColumnIndex == 1)
            {
                if (MessageBox.Show("确定要同意该请求？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes
                    && sqlDao.updateCardState(c_id, 1))
                {
                    msg.M_msg = "尊敬的用户您好！您申请的信用卡（卡号：" + c_id + "）已通过审核，请前往申请激活页面激活";
                    if (sqlDao.insertMsg(msg))
                    {
                        MessageBox.Show("已同意...");
                        dataGridView1.Rows.RemoveAt(e.RowIndex);
                    }
                    else
                    {
                        MessageBox.Show("同意失败...");
                    }
                }
            }

        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 0 && e.ColumnIndex != 1)
                return;
            string c_id = dataGridView2.Rows[e.RowIndex].Cells["卡号"].Value.ToString();
            string u_id = dataGridView2.Rows[e.RowIndex].Cells["申请人"].Value.ToString();

            Msg msg = new Msg();
            msg.M_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            msg.M_id = Utils.getTimeTicks();
            msg.U_id = u_id;
            //信用卡激活
            if (e.ColumnIndex == 0)
            {
                //取消
                if (MessageBox.Show("确定要驳回该请求？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes
                    && sqlDao.updateCardState(c_id, -1))
                {
                    msg.M_msg = "尊敬的用户您好！您申请激活的信用卡（卡号：" + c_id + "）未通过审核。";
                    if (sqlDao.insertMsg(msg))
                    {
                        MessageBox.Show("已驳回...");
                        dataGridView2.Rows.RemoveAt(e.RowIndex);
                    }
                    else
                    {
                        MessageBox.Show("驳回失败...");
                    }
                }
            }
            else if (e.ColumnIndex == 1) { }
            {
                if (MessageBox.Show("确定要同意该请求？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes
                    && sqlDao.updateCardState(c_id, 3))
                {
                    msg.M_msg = "尊敬的用户您好！您申请激活的信用卡（卡号：" + c_id + "）已激活";
                    if (sqlDao.insertMsg(msg))
                    {
                        MessageBox.Show("已同意...");
                        dataGridView2.Rows.RemoveAt(e.RowIndex);
                    }
                    else
                    {
                        MessageBox.Show("同意失败...");
                    }
                }
            }
        }

        private void get2()
        {
            DataSet ds = sqlDao.selectCard(2);
            if (ds.Tables[0].Rows.Count > 0)
            {
                dataGridView2.DataSource = ds.Tables[0].DefaultView;
                if (e2)
                {
                    DataGridViewButtonColumn column = new DataGridViewButtonColumn();
                    column.Text = "取 消";
                    column.Name = "编 辑";
                    column.UseColumnTextForButtonValue = true;
                    dataGridView2.Columns.Add(column);

                    column = new DataGridViewButtonColumn();
                    column.Text = "同 意";
                    column.Name = "编 辑";
                    column.UseColumnTextForButtonValue = true;
                    dataGridView2.Columns.Add(column);
                    e2 = false;
                }
            }
            else
            {
                MessageBox.Show("暂无用户申请...");
            }
        }
    }
}
